<template>
    <div style="width: 100%;height: 100%;background-color:#fff;margin: auto;margin-top: 100%">
        <van-loading size="28px" vertical>加载中...</van-loading>
    </div>
</template>

<script>
    import {loginByWX} from '@/api/api';
    import {Toast} from 'vant';
    import {setLocalStorage, getLocalStorage, removeLocalStorage} from '@/utils/local-storage';

    export default {
        name: "loginbywx",
        data() {
            return {
                wxcode:'',
                userInfo: {}
            }
        },
        created() {
            // let wechatCode = this.$route.params.code
            let test = window.location.search;
            let str = test.split("?code=")[1];
            let code = str.split("&")[0];
            this.wxcode = code

            this.wxlogin()
        },
        methods: {
            wxlogin(){
                const {pid} = getLocalStorage('pid');
                var param = {
                    code: this.wxcode
                };
                if (pid) {
                    param.parentId = pid;
                }

                loginByWX(param).then(res => {
                    this.userInfo = res.data.data.userInfo;
                    console.log(this.userInfo)
                    // if(this.userInfo.mobile){
                    //     setLocalStorage({
                    //         mobile: this.userInfo.mobile
                    //     });
                    // }
                    setLocalStorage({
                        Authorization: res.data.data.token,
                        avatar: this.userInfo.avatarUrl,
                        nickName: this.userInfo.nickName,
                        userId: this.userInfo.userId
                    });
                    // // 用完即删
                    // if (mobile) {
                    //     removeLocalStorage('mobile')
                    // }
                    if (pid) {
                        removeLocalStorage('pid')
                    }
                    window.location = '#/user/';
                }).catch(error => {
                    Toast.fail(error.data.errmsg);
                })
            }
        }
    }
</script>
<style scoped>

</style>
